Imports EnvDTE
Imports EnvDTE80

Public Class ProjectOptions

    Private _VStudio As DTE2
    Private _Instance As AddIn
    Private _Project As Project

    Public Sub New(ByVal vstudio As DTE2, ByVal instance As AddIn, ByVal project As EnvDTE.Project)
        Me.InitializeComponent()
        _VStudio = vstudio
        _Instance = instance
        _Project = project
    End Sub


    Private Sub ProjectOptionsLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Checker om der er et valgt projekt
        If _Project Is Nothing Then
            MsgBox("must select a project")
            Me.Close()
            Exit Sub
        End If

        Select Case ProjectOptionsManager.ProjectType(_Project)
            Case EProjectType.NotEnabled
                ProjectType.SelectedIndex = 0
            Case EProjectType.Files
                ProjectType.SelectedIndex = 1
            Case EProjectType.Templates
                ProjectType.SelectedIndex = 2
        End Select

        Me.ProjectName.Text = _Project.Name
        Me.UmbracoUser.Text = ProjectOptionsManager.UmbracoUser(_Project)
        Me.UmbracoPassword.Text = ProjectOptionsManager.UmbracoPassword(_Project)
        Me.UmbracoSite.Text = ProjectOptionsManager.UmbracoSite(_Project)
        Me.UmbracoFolder.Text = ProjectOptionsManager.UmbracoFolder(_Project)
        Me.FileExtension.Text = ProjectOptionsManager.FileExtension(_Project)
    End Sub


 
    Private Sub SaveOptionsClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveOptions.Click
        ProjectOptionsManager.UmbracoUser(_Project) = Me.UmbracoUser.Text
        ProjectOptionsManager.UmbracoPassword(_Project) = Me.UmbracoPassword.Text
        ProjectOptionsManager.UmbracoSite(_Project) = Me.UmbracoSite.Text
        ProjectOptionsManager.UmbracoFolder(_Project) = Me.UmbracoFolder.Text
        ProjectOptionsManager.FileExtension(_Project) = Me.FileExtension.Text

        Select Case Me.ProjectType.SelectedIndex
            Case 0
                ProjectOptionsManager.ProjectType(_Project) = EProjectType.NotEnabled
            Case 1
                ProjectOptionsManager.ProjectType(_Project) = EProjectType.Files
            Case 2
                ProjectOptionsManager.ProjectType(_Project) = EProjectType.Templates
        End Select
        Me.Close()
    End Sub

    Private Sub ProjectType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProjectType.SelectedIndexChanged
        Select Case ProjectType.SelectedIndex
            Case 0
                Me.UmbracoFolder.Visible = False
                Me.UmbracoFolderLabel.Visible = False
                Me.UmbracoOptionsLabel.Visible = False
                Me.UmbracoPassword.Visible = False
                Me.UmbracoPasswordLabel.Visible = False
                Me.UmbracoSite.Visible = False
                Me.UmbracoSiteLabel.Visible = False
                Me.UmbracoUser.Visible = False
                Me.UmbracoUserLabel.Visible = False
                Me.FileExtension.Visible = False
                Me.FileExtensionLabel.Visible = False

            Case 1 ' files
                Me.UmbracoFolder.Visible = True
                Me.UmbracoFolderLabel.Visible = True
                Me.UmbracoOptionsLabel.Visible = True
                Me.UmbracoPassword.Visible = True
                Me.UmbracoPasswordLabel.Visible = True
                Me.UmbracoSite.Visible = True
                Me.UmbracoSiteLabel.Visible = True
                Me.UmbracoUser.Visible = True
                Me.UmbracoUserLabel.Visible = True
                Me.FileExtension.Visible = True
                Me.FileExtensionLabel.Visible = True

            Case 2 ' templates
                Me.UmbracoFolder.Visible = False
                Me.UmbracoFolderLabel.Visible = False
                Me.UmbracoOptionsLabel.Visible = True
                Me.UmbracoPassword.Visible = True
                Me.UmbracoPasswordLabel.Visible = True
                Me.UmbracoSite.Visible = True
                Me.UmbracoSiteLabel.Visible = True
                Me.UmbracoUser.Visible = True
                Me.UmbracoUserLabel.Visible = True
                Me.FileExtension.Visible = False
                Me.FileExtensionLabel.Visible = False

        End Select
    End Sub
End Class


Class ProjectOptionsManager

    Public Shared Property ProjectType(ByVal project As Project) As EProjectType
        Get
            Try
                If GetValue(project, "ProjectType") = "" Then
                    Return EProjectType.NotEnabled
                End If

                Return CType([Enum].Parse(GetType(EProjectType), GetValue(project, "ProjectType")), EProjectType)
            Catch ex As Exception
                Trace.WriteLine("Could not parse projecttype, value was '" & GetValue(project, "ProjectType") & "'")
                Return EProjectType.NotEnabled
            End Try
        End Get
        Set(ByVal value As EProjectType)
            SetValue(project, "ProjectType", value.ToString)
        End Set
    End Property

    Public Shared Property FileExtension(ByVal project As Project) As String
        Get
            Return GetValue(project, "FileExtension")
        End Get
        Set(ByVal value As String)
            SetValue(project, "FileExtension", value)
        End Set
    End Property

    Public Shared Property UmbracoUser(ByVal project As Project) As String
        Get
            Return GetValue(project, "UmbracoUser")
        End Get
        Set(ByVal value As String)
            SetValue(project, "UmbracoUser", value)
        End Set
    End Property

    Public Shared Property UmbracoPassword(ByVal project As Project) As String
        Get
            Return GetValue(project, "UmbracoPassword")
        End Get
        Set(ByVal value As String)
            SetValue(project, "UmbracoPassword", value)
        End Set
    End Property

    Public Shared Property UmbracoSite(ByVal project As Project) As String
        Get
            Return GetValue(project, "UmbracoSite")
        End Get
        Set(ByVal value As String)
            SetValue(project, "UmbracoSite", value)
        End Set
    End Property

    Public Shared Property UmbracoFolder(ByVal project As Project) As String
        Get
            Return GetValue(project, "UmbracoFolder")
        End Get
        Set(ByVal value As String)
            SetValue(project, "UmbracoFolder", value)
        End Set
    End Property

    Private Shared Sub SetValue(ByRef project As Project, ByVal key As String, ByVal value As String)
        Try
            project.Globals.VariableValue(key) = value
            project.Globals.VariablePersists(key) = True
        Catch ex As Exception
            Console.WriteLine(String.Format("Could not set global value to {0} for key {1} on project {2}", value, key, project.Name))
        End Try
    End Sub

    Private Shared Function GetValue(ByRef project As Project, ByVal key As String) As String
        Try
            Return CStr(project.Globals.VariableValue(key))
        Catch ex As Exception
            Console.WriteLine(String.Format("Could not read key {0} from project {1}", key, project.Name))
            Return ""
        End Try
    End Function

End Class
